package com.glory.dgcpcpda.activity.mfg;


import android.os.Build;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.BinInfoListAdapter;
import com.glory.dgcpcpda.adapter.LastBatchAdapter;
import com.glory.dgcpcpda.adapter.MesBinInfoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.BinInfoEntity;
import com.glory.dgcpcpda.entity.EdcEntity;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FTDataSortActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpWork)
    EditText etEqpWork;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvScarpNum)
    TextView tvScarpNum;
    @BindView(R.id.tvGetData)
    TextView tvGetData;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.tvSynchronous)
    TextView tvSynchronous;
    @BindView(R.id.rvBINInfoList)
    RecyclerView rvBINInfoList;
    @BindView(R.id.rvLastBatchList)
    RecyclerView rvLastBatchList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTotal)
    TextView tvTotal;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.tvSampleQty)
    TextView tvSampleQty;
    private MesBinInfoAdapter mBinInfoListAdapter;
    private LotInfoEntity mLoInfoEntity;
    private EdcEntity body;
    private LastBatchAdapter lastBatchAdapter;
    private List<EdcSetBinEntity> GBinList = new ArrayList<>();
    private List<EdcSetBinEntity> binData = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ftdata_sort;
    }

    @Override
    protected void initView() {
        listTv1.setText("BIN名称");
        listTv2.setText("BIN描述");
        listTv3.setText("数量");
        listTv4.setText("占比良率");
        initRecyclerView(rvBINInfoList, this);
        initRecyclerView(rvLastBatchList, this);
        tvTitle.setText("在线整合");
        setMainLayout(llMain);
        mBinInfoListAdapter = new MesBinInfoAdapter(null);
        mBinInfoListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mBinInfoListAdapter.isFirstOnly(true);
        rvBINInfoList.setAdapter(mBinInfoListAdapter);
        lastBatchAdapter = new LastBatchAdapter(null);
        lastBatchAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lastBatchAdapter.isFirstOnly(false);
        rvLastBatchList.setAdapter(lastBatchAdapter);

        initEdit(etLot, s -> LotUtils.searchLotInfo(FTDataSortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                mBinInfoListAdapter.setMainQTY(0);
                tvNum.setText(response.getLOT().getMAINQTY());
                mBinInfoListAdapter.setMainQTY(Integer.parseInt(response.getLOT().getMAINQTY()));
                tvStep.setText(response.getLOT().getSTEPNAME() + "/" + response.getLOT().getSTEPDESC());
                String state = response.getLOT().getSTATE();
                if(state.equals("正在作业")){
                    state = state + " - " + response.getLOT().getEQUIPMENTID();
                }
                tvLotState.setText(state);
                mLoInfoEntity = response;
                testResultCompared(s);
                requestEdcData();
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));
    }

    private void testResultCompared(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<Object>>
                (new TypeToken<BaseResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<Object>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("数据比对成功", true);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "TestResultCompared");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID", s);
//        NetUtils.requestPostNet("CPC.FTDATACOMPARISON", "FTDATACOMPARISON",this, map, callBack);

    }

    private void requestEdcData() {
//        String ss = "{\"Response\":{\"Header\":{\"TRANSACTIONID\":\"\",\"RESULT\":\"SUCCESS\"},\"Body\":{\"LOTID\":\"20K2410-1-019\",\"MAINQTY\":21142,\"PARTNAME\":\"FP36003GD\",\"STEPNAME\":\"T1100\",\"DAWBQTY\":8,\"EDCSETLIST\":[{\"EDCSETNAME\":\"ALL_BIN\",\"EDCSETDESCRIPTION\":\"ALL_BIN\",\"EDCTYPE\":\"BIN\",\"EDCSETBINLINELIST\":[{\"NAME\":\"BIN1\",\"DESCRIPTION\":\"良品不分档\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN1_1\",\"DESCRIPTION\":\"良品A档\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"ACTIONPARAM1\":\"A\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN1_2\",\"DESCRIPTION\":\"良品B档\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"ACTIONPARAM1\":\"B\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN1_3\",\"DESCRIPTION\":\"良品C档\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"ACTIONPARAM1\":\"C\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN1_4\",\"DESCRIPTION\":\"良品D档\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"ACTIONPARAM1\":\"D\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN1P\",\"DESCRIPTION\":\"良品BIN组\",\"SPECTYPE\":\"PERCENT\",\"LSL\":\"97.5\",\"BINTYPE\":\"G\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN2\",\"DESCRIPTION\":\"OS不良品\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"0.8\",\"BINTYPE\":\"F\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN3\",\"DESCRIPTION\":\"FT不良品\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN3_4_5\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN3_4_5\",\"DESCRIPTION\":\"FT不良BIN组\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"2.5\",\"BINTYPE\":\"G\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN4\",\"DESCRIPTION\":\"FT不良品\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN3_4_5\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN5\",\"DESCRIPTION\":\"FT不良品\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN3_4_5\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN6\",\"DESCRIPTION\":\"QA不良品\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"0.00\",\"BINTYPE\":\"F\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN7\",\"DESCRIPTION\":\"印字不良\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN7_8\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN8\",\"DESCRIPTION\":\"引脚不良\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN7_8\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN9\",\"DESCRIPTION\":\"少数（虚拟BIN）\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN9_10\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN10\",\"DESCRIPTION\":\"报废（虚拟BIN）\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"F\",\"BINGROUP\":\"BIN9_10\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN12\",\"DESCRIPTION\":\"未测BIN\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"BINACTION\":\"Split\"},{\"NAME\":\"BIN13\",\"DESCRIPTION\":\"掉料(虚拟BIN)\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"0.50\",\"BINTYPE\":\"F\",\"BINACTION\":\"Scrap\"},{\"NAME\":\"BIN7_8\",\"DESCRIPTION\":\"印字引脚不良\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"0.15\",\"BINTYPE\":\"G\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN9_10\",\"DESCRIPTION\":\"少数报废超标\",\"SPECTYPE\":\"PERCENT\",\"USL\":\"0.15\",\"BINTYPE\":\"G\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN99\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"O\",\"BINACTION\":\"None\"},{\"NAME\":\"BIN88\",\"DESCRIPTION\":\"并出尾数\",\"SPECTYPE\":\"PERCENT\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"BINACTION\":\"None\",\"VALUE\":\"123.0\"},{\"NAME\":\"BIN77\",\"DESCRIPTION\":\"并出尾数\",\"SPECTYPE\":\"NUMBER\",\"BINTYPE\":\"P\",\"BINGROUP\":\"BIN1P\",\"BINACTION\":\"None\",\"VALUE\":\"123.0\"}]}],\"LOTLIST\":[{\"LOTID\":\"20K2410-1-012M\",\"MAINQTY\":2000,\"SUBQTY\":1}]}}}";
//        BaseResponseBean<EdcEntity> dd = (new Gson()).fromJson(ss, new TypeToken<BaseResponseBean<EdcEntity>>() {
//        }.getType());
//        if (dd.isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
//            body = dd.getResponse().getBody();
//            lastBatchAdapter.setNewData(body.getLOTLIST());
//            tvScarpNum.setText(String.valueOf(body.getDAWBQTY()));
//            if (body.getEDCSETLIST().size() > 0) {
//                if (body.getEDCSETLIST().get(0).getEDCSETITEMLINELIST().size() > 0) {
//                    for (EdcSetBinEntity edcSetBinEntity : body.getEDCSETLIST().get(0).getEDCSETITEMLINELIST()) {
//                        BinInfoEntity.BinInfo entity = new BinInfoEntity.BinInfo();
//                        entity.setBINNO(edcSetBinEntity.getNAME());
//                        entity.setBINNAME(edcSetBinEntity.getDESCRIPTION());
//                        try {
//                            entity.setBINQTY(edcSetBinEntity.getVALUE().equals("") ? 0.0 : Double.parseDouble(edcSetBinEntity.getVALUE()));
//                        }catch (Exception e){
//                            entity.setBINQTY(0.0);
//                        }
//                        binData.add(entity);
//                    }
//                }
//
//            }
//        }
        FreeUI_EntityCallBack<BaseResponseBean<EdcEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EdcEntity>>
                (new TypeToken<BaseResponseBean<EdcEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EdcEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    GBinList.clear();
                    binData.clear();
                    body = response.body().getResponse().getBody();
                    lastBatchAdapter.setNewData(body.getLOTLIST());
                    tvScarpNum.setText(String.valueOf(body.getDAWBQTY()));
                    tvTotal.setText(String.valueOf(body.getTOTALQTY()));
                    tvSampleQty.setText(String.valueOf(body.getSAMPLEQTY()));
                    if (body.getEDCSETLIST().size() > 0) {
                        if (body.getEDCSETLIST().get(0).getEDCSETITEMLINELIST().size() > 0) {
                            for (EdcSetBinEntity edcSetBinEntity : body.getEDCSETLIST().get(0).getEDCSETITEMLINELIST()) {
                                if (edcSetBinEntity.getBINTYPE().equals("G")){
                                    GBinList.add(edcSetBinEntity);
                                }else {
                                    binData.add(edcSetBinEntity);
                                }

                            }
                        }
//                        mBinInfoListAdapter.setNewData(data);
                    }

                    if (binData.size() <= 0) {
                        showToast("MES返回BIN信息为空，请联系系统管理员", false);
                        return;
                    }
                    getData();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EdcEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "CPCGETEDCCURRENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID", getText(etLot));
        NetUtils.requestPostNet("CPC.CPCGETEDCCURRENT", "TRANSFERMES",this, map, callBack);
    }

    @Override
    protected void initData() {
//        requestEdcData();
    }


    @OnClick({R.id.tvBaseSubmit, R.id.tvGetData, R.id.tvSynchronous})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (actionControl("Submit26")) return;

                if (mLoInfoEntity == null) {
                    showToast("未获取到批次信息，请重新扫描批次号", false);
                    return;
                }

                if (mBinInfoListAdapter.getData().size() <= 0) {
                    showToast("未维护EDC项集规则，请联系测试工艺维护EDC规则", false);
                    return;
                }

                double sum = 0;
                for (EdcSetBinEntity entity : mBinInfoListAdapter.getData()) {
                    if (entity.getNAME().equals("BIN99") || entity.getNAME().equals("BIN88") || entity.getNAME().equals("BIN77")) {
                        continue;
                    }
                    sum += entity.getVALUE().equals("") ? 0 : Integer.parseInt(entity.getVALUE());
                }

                int lotNum = Integer.parseInt(tvTotal.getText().toString());
//                int lotNum = 21142;
                if (lastBatchAdapter.getData().size() > 0) {
                    for (EdcEntity.EdcSet edcSet : lastBatchAdapter.getData()) {
                        if (edcSet.getSUBQTY() != 1) {
                            lotNum -= Integer.parseInt(edcSet.getMAINQTY());
                        }
                    }
                }

                if (sum != lotNum) {
                    showConfirmDialog("Total不等于BIN的总数！差值：" + (lotNum - sum));
                    return;
                }

                showSubmitDialog(() -> submit(getText(etLot), getText(etEqpWork), mLoInfoEntity, mBinInfoListAdapter.getData()));

                break;
            case R.id.tvGetData:
                if (getText(etLot).equals("")) {
                    showToast("批次ID不能为空", false);
                    return;
                }

                if (binData.size() <= 0) {
                    showToast("MES返回BIN信息为空，请联系系统管理员", false);
                    return;
                }

                getData();
//                String ss = "{\n" +
//                        "  \"Response\": {\n" +
//                        "    \"Header\": {\n" +
//                        "      \"TRANSACTIONID\": \"\",\n" +
//                        "      \"RESULT\": \"SUCCESS\",\n" +
//                        "      \"RESULTCODE\": null,\n" +
//                        "      \"RESULTMESSAGE\": null\n" +
//                        "    },\n" +
//                        "    \"Body\": {\n" +
//                        "      \"HBINLIST\": [\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN1\",\n" +
//                        "          \"BINNAME\": \"BIN1\",\n" +
//                        "          \"BINQTY\": 578.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN2\",\n" +
//                        "          \"BINNAME\": \"BIN2\",\n" +
//                        "          \"BINQTY\": 375.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN3\",\n" +
//                        "          \"BINNAME\": \"BIN3\",\n" +
//                        "          \"BINQTY\": 28.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN4\",\n" +
//                        "          \"BINNAME\": \"BIN4\",\n" +
//                        "          \"BINQTY\": 273.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN5\",\n" +
//                        "          \"BINNAME\": \"BIN5\",\n" +
//                        "          \"BINQTY\": 202.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN6\",\n" +
//                        "          \"BINNAME\": \"BIN6\",\n" +
//                        "          \"BINQTY\": 0.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN7\",\n" +
//                        "          \"BINNAME\": \"BIN7\",\n" +
//                        "          \"BINQTY\": 135.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN8\",\n" +
//                        "          \"BINNAME\": \"BIN8\",\n" +
//                        "          \"BINQTY\": 40.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN1_2\",\n" +
//                        "          \"BINNAME\": \"BIN1\",\n" +
//                        "          \"BINQTY\": 4567.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN1_3\",\n" +
//                        "          \"BINNAME\": \"BIN1\",\n" +
//                        "          \"BINQTY\": 2622.0\n" +
//                        "        },\n" +
//                        "        {\n" +
//                        "          \"BINNO\": \"BIN1_4\",\n" +
//                        "          \"BINNAME\": \"BIN1\",\n" +
//                        "          \"BINQTY\": 12322.0\n" +
//                        "        }\n" +
//                        "      ]\n" +
//                        "    }\n" +
//                        "  }\n" +
//                        "}";
//                BaseResponseBean<BinInfoEntity> dd = (new Gson()).fromJson(ss, new TypeToken<BaseResponseBean<BinInfoEntity>>() {
//                }.getType());
//                if (dd.isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
//                    List<BinInfoEntity.BinInfo> hbinlist = dd.getResponse().getBody().getHBINLIST();
//                    for (int i = 0; i < hbinlist.size(); i++) {
//                        for (int j = 0; j < binData.size(); j++) {
//                            if (hbinlist.get(i).getBINNO().equals(binData.get(j).getBINNO())){
//                                binData.get(j).setBINQTY(hbinlist.get(i).getBINQTY());
//                            }
//                        }
//                    }
//                    mBinInfoListAdapter.setNewData(binData);
//                }
                break;
            case R.id.tvSynchronous:
                for (EdcSetBinEntity entity : mBinInfoListAdapter.getData()) {
                    if (entity.getNAME().equals("BIN99")) {
                        synchronous(mBinInfoListAdapter.getData().indexOf(entity));
                        return;
                    }
                }
                showToast("未找到BIN99，无法同步", false);
                break;
        }
    }

    private void getData(){
        FreeUI_EntityCallBack<BaseResponseBean<BinInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BinInfoEntity>>
                (new TypeToken<BaseResponseBean<BinInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (!tvSampleQty.getText().toString().equals("")){
                        for (EdcSetBinEntity edcData:binData){
                            if (edcData.getNAME().equals("BIN11")){
                                edcData.setVALUE(tvSampleQty.getText().toString());
                            }
                        }
                    }

                    if (response.body().getResponse().getBody().getHBINLIST().size() <= 0) {
                        showConfirmDialog("EAP返回数据为空");
                        if (mBinInfoListAdapter.getData().size() <= 0){
                            mBinInfoListAdapter.setNewData(binData);
                        }
                        return;
                    }

                    List<BinInfoEntity.BinInfo> hbinlist = response.body().getResponse().getBody().getHBINLIST();
                    for (int i = 0; i < hbinlist.size(); i++) {
                        for (int j = 0; j < binData.size(); j++) {
                            if (hbinlist.get(i).getBINNO().equals(binData.get(j).getNAME())){
                                binData.get(j).setVALUE(String.valueOf(hbinlist.get(i).getBINQTY()));
                            }
                        }
                    }
                    mBinInfoListAdapter.setNewData(binData);

//                            BinInfoEntity.BinInfo binInfo9 = new BinInfoEntity.BinInfo("BIN9", "少数(虚拟BIN)");
//                            BinInfoEntity.BinInfo binInfo10 = new BinInfoEntity.BinInfo("BIN10", "报废(虚拟BIN)");
                    //MES 不需要 BIN11 做样品bin
//                            BinInfoEntity.BinInfo binInfo11 = new BinInfoEntity.BinInfo("BIN11", "样品");
//                            BinInfoEntity.BinInfo binInfo12 = new BinInfoEntity.BinInfo("BIN12", "未测BIN");
//                            BinInfoEntity.BinInfo binInfo13 = new BinInfoEntity.BinInfo("BIN13", "掉料(虚拟BIN)");
//                            BinInfoEntity.BinInfo binInfo99 = new BinInfoEntity.BinInfo("BIN99", "封装不良");
//                            hbinlist.add(binInfo9);
//                            hbinlist.add(binInfo10);
//                            hbinlist.add(binInfo11);
//                            hbinlist.add(binInfo12);
//                            hbinlist.add(binInfo13);
//                            hbinlist.add(binInfo99);
//                            mBinInfoListAdapter.notifyDataSetChanged();


                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "RTM.BINRESULTREQUEST");
            }
        };

        JsonObject map = new JsonObject();
//                map.addProperty("EQUIPMENTID",getText(etEqpWork));
        map.addProperty("LOTID", getText(etLot));
        NetUtils.requestPostNet("RTM.BINRESULTREQUEST","BINRESULTREQUEST",this,map,callBack);
    }

    private void synchronous(int position) {
        List<EdcSetBinEntity> data = mBinInfoListAdapter.getData();
        //修改BIN2得数量
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getNAME().equals("BIN2")) {
                EdcSetBinEntity entity = data.get(i);
                int value = (entity.getVALUE().equals("") ? 0 : Integer.parseInt(entity.getVALUE())) - body.getDAWBQTY();
                entity.setVALUE(String.valueOf(value));
                break;
            }
        }
        //修改BIN99得数量
        mBinInfoListAdapter.getData().get(position).setVALUE(String.valueOf(body.getDAWBQTY()));
        mBinInfoListAdapter.notifyDataSetChanged();
        showToast("同步成功", true);
        tvGetData.setVisibility(View.GONE);
        tvSynchronous.setVisibility(View.GONE);
    }

    private void submit(String lotID, String eqpID, LotInfoEntity lotInfoEntity, List<EdcSetBinEntity> binList) {
        FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                (new TypeToken<BaseResponseBean<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    FreeApi_ToastUtils.showFreeToast("提交成功", FTDataSortActivity.this, true, 0);
                    String sblFlag="";String msg="";
                    if(response.body().getResponse().getBody().get("SBLFLAG")!=null){
                        sblFlag= response.body().getResponse().getBody().get("SBLFLAG").getAsString();
                    }
                    if(response.body().getResponse().getBody().get("MSG")!=null){
                        msg= response.body().getResponse().getBody().get("MSG").getAsString();
                    }
                    if(sblFlag.equals("Y")){
                        showConfirmDialog(msg);
                    }
                    FTDataSortActivity.this.finish();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                super.onError(response);
                loadError(response.getException(), "CPCSAVEEDCDATA");
            }
        };
//        JsonObject map = new JsonObject();
//        map.addProperty("ACTIONTYPE", "Integration");
//        map.addProperty("TESTTYPE", "");
//        map.addProperty("LOTID", lotID);
//        map.addProperty("PRODUCTMODEL", lotInfoEntity.getPRODUCTMODEL());
//        map.addProperty("WIRINGNO", lotInfoEntity.getWIRINGNO());
//        map.addProperty("SEALNO", lotInfoEntity.getSEALNO());
//        map.addProperty("CHIPNAME", lotInfoEntity.getCHIPNAME());
//        map.addProperty("WIRESPEC", lotInfoEntity.getWIRESPEC());
//        map.addProperty("EQUIPMENTID", eqpID);
//        map.addProperty("CHIPLOT", lotInfoEntity.getCHIPID());
//        map.addProperty("LOTTYPE", lotInfoEntity.getLOT().getLOTTYPE());
//        map.addProperty("PACKAGETYPE", lotInfoEntity.getLOT().getMAINMATTYPE());
//        map.addProperty("CUSTOMERCODE", lotInfoEntity.getLOT().getCUSTOMERCODE());
//        map.addProperty("LOTCOUNT", lotInfoEntity.getLOT().getMAINQTY());
//        map.addProperty("OBJECTRRN", lotInfoEntity.getLOT().getOBJECTRRN());
//        map.add("HBINLIST", new JsonParser().parse(new Gson().toJson(binList.addAll(GBinList))).getAsJsonArray());
//        NetUtils.requestPostNet("RTM.INTEGRATIONSUBMIT", "INTEGRATIONSUBMIT", this, map, callBack);

        JsonObject map = new JsonObject();
        map.addProperty("LOTID",getText(etLot));
        map.addProperty("ISTEMP",false);
        map.addProperty("ISRMS",true);
        map.addProperty("TOTALQTY",tvTotal.getText().toString());
        List<EdcEntity.EdcSet> edcsetlist = body.getEDCSETLIST();
        binList.addAll(GBinList);
        edcsetlist.get(0).setEDCSETITEMLINELIST(binList);
        map.add("EDCSETLIST",new JsonParser().parse(new Gson().toJson(edcsetlist)).getAsJsonArray());
        NetUtils.requestPostNet("CPC.CPCSAVEEDCDATA","TRANSFERMES",this,map,callBack);
    }
}
